const DB = require('../db')
const $sql = require('../map/lotteryMap')

const LotteryDao = {
    // 查询数据库
  sqlAsync: async function (opts) {
    try {
      let results = await DB.querySql(opts.sql, opts.param)
      return results
    } catch (err) {
      throw new Error(err)
    }
  },
  // 获取作者id获取该作者预测观点列表,取最近的50条记录
  getForecastListByMediaId: async function (req) {
    return this.sqlAsync({
      sql: $sql.getForecastListByMediaId,
      param: [req.timeString, req.mediaId]
    })
  },
  // 更新服务人数
  updateMediaService: function (req) {
    this.sqlAsync({
      sql: $sql.updateMediaService,
      param: req.get_user_id
    })
    console.log(`updateMediaService success`)
  },
  // 更新交易信息
  updateTransaction: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.updateTransaction,
      param: [req.bank_type, req.cash_fee, req.is_subscribe, req.transaction_id, req.status, req.out_trade_no]
    })
    console.log('update transaction record success', res)
  },
  // 更新被认可数值
  updateForecastRecognizedNum: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.updateForecastRecognizedNum,
      param: req.forecast_id
    })
    console.log(`updateForecastRecognizedNum: ${res[0]}`)
  },
  // 更新账户
  updateAccountMoney: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.updateAccountMoney,
      param: [req.add_money, req.writer_id]
    })
    console.log(`updateAccountMoney: ${res[0]}`)
  },
  // 查询交易记录
  queryByOutTradeNo: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.queryByOutTradeNo,
      param: req.out_trade_no
    })
    // console.log(`query By OutTradeNo ${res[0]}`)
    return res[0]
  },
  // 预交易记录入库
  insertTransactionAndForecastId: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.insertTransactionandforecastId,
      param: [req.forecast_id, req.out_trade_no, req.openid, req.tax_fee, req.total_fee, req.user_id, req.getUserId, req.status, req.prepay_id]
    })
    console.log('insertTransactionandforecastId:', res.insertId)
  },
// base 用户实存存储在了数据库表中
  isUserInfoBaseSaveinWriter: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.isUserInfoSaveinWriter,
      param: req.openid
    })
    console.log('isUserInfoBaseSaveinWriter:', res[0].id)
    if (typeof res[0] !== 'undefined') {
      return res[0].id
    } else {
      let userId = this.baseUserRegister(req)
      return userId
    }
  },
  baseUserRegister: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.insertBaseUserInfointoWriter,
      param: [req.openid]
    })
    return res.insertId
  },
// 用户实存存储在了数据库表中
  isUserInfoSaveinWriter: async function (req) {
    let res = await this.sqlAsync({
      sql: $sql.isUserInfoSaveinWriter,
      param: req.openid
    })
    console.log('isUserInfoSaveinWriter:', res[0], typeof res[0])
    if (typeof res[0] !== 'undefined') {
      return res[0].id
    } else {
      let userId = await this.userRegister(req)
      return userId
    }
  },
// 用户注册
  userRegister: async function (req) {
    // console.log('userRegister:', req)
    let res = await this.sqlAsync({
      sql: $sql.insertUserInfointoWriter,
      param: [req.openid, req.nickname, req.sex, req.province, req.city, req.country, req.headimgurl, String(req.privilege)]
    })
    // console.log('userRegister:', res)
    return res.insertId
  },
// 获取预测观点列表
  getForecastListOption: async function (req) {
    return this.sqlAsync({
      sql: $sql.getForecastListOption,
      param: req.timeString
    })
  },
// 获取预测信息
  getForecastInfo: async function (req) {
    return this.sqlAsync({
      sql: $sql.getForecastInfo,
      param: req.forecast_id
    })
  },
// 获取媒体信息
  getMediaInfoBymediaId: async function (req) {
    return this.sqlAsync({
      sql: $sql.getMediaInfoBymediaId,
      param: req.media_id
    })
  },
  // 用户是否支付过预测
  isUserPayForecast: async function (req) {
    return this.sqlAsync({
      sql: $sql.isUserPayForecast,
      param: [req.forecast_id, req.userId]
    })
  },
  // 创建预测日志
  createForecastLog: function (req) {
    this.sqlAsync({
      sql: $sql.createForecastLog,
      param: [+req.user_writer_id > 0 ? +req.user_writer_id : 0, req.media_id, req.forecast_id]
    })
  },
  // 更新阅读数
  updateForecastRead: function (req) {
    this.sqlAsync({
      sql: $sql.updateForecastRead,
      param: +req.forecast_id
    })
  }
}

module.exports = LotteryDao
